package com.sais.manage.boot.daily.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.utils.UUIDUtils;
import com.sais.manage.boot.daily.dto.ApplicationDTO;
import com.sais.manage.boot.daily.dto.ApplicationSearchDTO;
import com.sais.manage.boot.daily.dto.ApplicationSaveDTO;
import com.sais.manage.boot.daily.entity.Application;
import com.sais.manage.boot.daily.mapper.ApplicationMapper;
import com.sais.manage.boot.daily.service.IApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 宿舍管理系统-申请 service 实现类
 *
 * @author zwp
 * @date 2020/2/2
 **/
@Service
@Slf4j
public class ApplicationServiceImpl implements IApplicationService {

    @Resource
    private ApplicationMapper applicationMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveApplication(ApplicationSaveDTO applicationSaveDTO) {
        String ukId = applicationSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            createApplication(applicationSaveDTO);
        } else {
            updateApplication(applicationSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean createApplication(ApplicationSaveDTO applicationSaveDTO) {
        Application application = applicationSaveDTO.convert();
        application.setUkId(UUIDUtils.getLower());
        if (StringUtils.isEmpty(application.getStatus())) {
            application.setStatus(0);
        }
        applicationMapper.createApplication(application);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateApplication(ApplicationSaveDTO applicationSaveDTO) {
        Application application = applicationSaveDTO.convert();
        application.setGmtModified(LocalDateTime.now());
        applicationMapper.updateById(applicationSaveDTO.convert());
        return true;

    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteApplication(String meetingSaveDTO) {
        applicationMapper.deleteApplication(meetingSaveDTO);
        return true;
    }

    @Override
    public ApplicationDTO selectApplication(String meetingId) {
        ApplicationDTO applicationDTO = applicationMapper.selectApplication(meetingId);
        return applicationDTO;
    }

    @Override
    public Page<ApplicationDTO> listApplication(Integer current, Integer size, ApplicationSearchDTO searchDTO) {
        Page<ApplicationDTO> page = new Page<>(current, size);
        return applicationMapper.listApplication(page, searchDTO);
    }

    @Override
    public Page<ApplicationDTO> listApplicationByManager(String userId, Integer current, Integer size, ApplicationSearchDTO searchDTO) {
        Page<ApplicationDTO> applications = applicationMapper.listApplicationByManage(new Page<ApplicationDTO>(current, size), userId, searchDTO);
        return applications;
    }

    @Override
    public boolean updateApplicationAcceptStatus(String applicationId) {
        Application application = Application.builder()
                .ukId(applicationId)
                .status(1)
                .build();
        applicationMapper.updateById(application);
        return true;
    }

    @Override
    public boolean updateApplicationRefuseStatus(String applicationId) {
        Application application = Application.builder()
                .ukId(applicationId)
                .status(2)
                .build();
        applicationMapper.updateById(application);
        return true;
    }

    @Override
    public List<ApplicationDTO> listApplicationByUserId(String userId) {
        List<ApplicationDTO> applications = applicationMapper.listApplicationByUserId(userId);
        return applications;
    }
}
